package controllers

import (
	"github.com/astaxie/beego"
	"shuxianghui/models"
	"HTshuxianghui/htmodels"
)
//读书会简介为过滤
type HdController struct {
	beego.Controller
}

func (c *HdController)GetUnCheckedList() {
	uid, _ := c.GetInt("id")
	if uid == 0 {
		Json_Message(&c.Controller, 1, "not permited")
	}
	user := models.Users{Id:uid}
	if err := htmodels.HTUsersGetDetail(&user); err != nil {
		Json_Message(&c.Controller, 1, err.Error())
	}
	if user.UserType <= 1 {
		Json_Message(&c.Controller, 1, "user type not right")
	}

	Status := 0
	limit := 200
	ArticleType := 7
	if user.UserType > 2 {
		ArticleType = -1
	}
	offset := int64(0)
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "Title", "Author", "EditTime", "Introduction", "ArticleType", "Status", "BrowseCount", "StartCount", "WatchCount", "UserId"}
	Article := []models.Article{}
	uid = 0
	htmodels.HTArticleGetList(&Article, fileds, Status, ArticleType, offset, limit, search, "Id", orderby, user.CityId, uid)

	c.Data["json"] = &Article
	c.ServeJSON()
}

func (c *HdController)PassOrnot() {
	uid, _ := c.GetInt("id")
	if uid == 0 {
		Json_Message(&c.Controller, 1, "not permited")
	}
	user := models.Users{Id:uid}
	if err := htmodels.HTUsersGetDetail(&user); err != nil {
		Json_Message(&c.Controller, 1, err.Error())
	}
	if user.UserType <= 1 {
		Json_Message(&c.Controller, 1, "user type not right")
	}

	articleid, _ := c.GetInt("articleid")
	if articleid == 0 {
		Json_Message(&c.Controller, 1, "article id not right")
	}
	Article := models.Article{Id:articleid}
	if err := htmodels.HTArticleGetDetail(&Article); err != nil {
		Json_Message(&c.Controller, 1, "get article not right")
	}
	if Article.Status != 0 {
		Json_Message(&c.Controller, 1, "article status is others, do not change it")
	}
	if Article.CityId != user.CityId {
		Json_Message(&c.Controller, 1, "not the same adress")
	}

	pass, _ := c.GetInt("pass")

	if pass == 1 {
		if Article.ArticleType == 7 {
			Article.Status = 3
		} else {
			if user.UserType > 2 {
				Article.Status = 3
			} else {
				Json_Message(&c.Controller, 1, "article not permite")
			}
		}
	} else {
		if Article.ArticleType == 7 {
			Article.Status = 1
		} else {
			if user.UserType > 2 {
				Article.Status = 1
			} else {
				Json_Message(&c.Controller, 1, "article not permite")
			}
		}
	}
	if err := htmodels.HTArticleUpdate(&Article); err != nil {
		Json_Message(&c.Controller, 1, "article update failed")
	}
	Json_Message(&c.Controller, 0, "submite sucess")
}

func (c *HdController)GetShortByUserid() {
	uid, _ := c.GetInt("id")
	if uid == 0 {
		Json_Message(&c.Controller, 1, "not permited")
	}
	user := models.Users{Id:uid}
	if err := htmodels.HTUsersGetDetail(&user); err != nil {
		Json_Message(&c.Controller, 1, err.Error())
	}
	Article:=models.Article{CityId:user.CityId}
	if err:=htmodels.HTArticleGetDushuByCity(&Article);err!=nil{
		Json_Message(&c.Controller, 1, "get short wrong"+err.Error())
	}
	c.Data["json"]=&Article
	c.ServeJSON()
}